package com.liu.communitysystemend.module.memu.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.communitysystemend.exception.CommonException;
import com.liu.communitysystemend.exception.SavaException;
import com.liu.communitysystemend.module.memu.domain.Menu;
import com.liu.communitysystemend.module.memu.mapper.MenuMapper;
import com.liu.communitysystemend.module.memu.service.MenuService;
import com.liu.communitysystemend.utils.AuthUtils;
import com.liu.communitysystemend.utils.Result;
import com.liu.communitysystemend.utils.WStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
*
*/
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private  MenuMapper menuMapper;


    @Override
    public List<Menu> getMenuList(String role) {
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Menu::getRole,role);
        wrapper.eq(Menu::getStatus,1);
        wrapper.orderBy(true,true);
        List<Menu> menus = menuMapper.selectList(wrapper);
        return menus;
    }

    @Override
    public Result<Menu> getMenuById(String id) {
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Menu::getId,id);
        Menu menu = menuMapper.selectOne(wrapper);
        if (menu!=null){
            return Result.success(menu);
        }
        return Result.failed("id不能为空",menu);
    }

    @Override
    public Result<Menu> update(Menu menu) {
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Menu::getId,menu.getId());
        int update = menuMapper.update(menu, wrapper);
        if (update>0){
            return Result.success(menu);
        }
        throw new CommonException(502,"更新失败");
    }

    @Override
    public Menu add(Menu menu) {
        if(menu ==null){
            throw new SavaException("插入用户失败,菜单的类型实体为空");
        }
        int insert=menuMapper.insert(menu);
        if(insert>0){
            return menu;
        }
        throw new SavaException("插入用户失败");
    }

    @Override
    public Page<Menu> getPage(Menu menu) {
        LambdaQueryWrapper<Menu> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Menu::getIsDeleted,0);
        if (!WStringUtils.isBlank(menu.getRole())){
            wrapper.like(Menu::getRole,menu.getRole());
        }
        Page<Menu> page=new Page<>(menu.getPage().getPage(),menu.getPage().getSize());
        Page<Menu> poiPage=(Page<Menu>) menuMapper.selectPage(page,wrapper);
        return poiPage;
    }

    @Override
    public Integer deleteById(String id) {
        Menu menu = selectOneById(id);
        if(menu==null){
            throw new CommonException(4004,"找不到id为:"+id+"的数据");
        }
        int i=menuMapper.deleteById(id);
        return i;
    }

    public Menu selectOneById(String id){
        Menu menu=menuMapper.selectById(id);
        return  menu;
    }
}
